//登录页的相关配置
import axios from "axios"
import Vue from "../main"

export default {
    namespaced: true,
    actions: {
        sendCode(context, phone) {
            let formData = new FormData()
            formData.append("phone", phone)
            axios.post("/api/send", formData).then(res => {
                this._vm.$message(res.data.data)
            })
        },
        codeLogin(context, data) {
            console.log(Vue);
            let formData = new FormData()
            formData.append("phone", data.phone)
            formData.append("code", data.code)
            axios.post("/api/loginPhone", formData).then(res => {
                let resData = res.data.data;
                if (resData != null) {
                    switch (resData.role) {
                        case "0": //管理员
                            Vue.$cookies.set("cname", resData.adminName);
                            Vue.$cookies.set("cid", resData.adminId);
                            Vue.$cookies.set("role", 0);
                            Vue.$router.push({ path: "/index" }); //跳转到首页
                            break;
                        case "1": //教师
                            Vue.$cookies.set("cname", resData.teacherName);
                            Vue.$cookies.set("cid", resData.teacherId);
                            Vue.$cookies.set("role", 1);
                            Vue.$router.push({ path: "/index" }); //跳转到教师用户
                            break;
                        case "2": //学生
                            Vue.$cookies.set("cname", resData.studentName);
                            Vue.$cookies.set("cid", resData.studentId);
                            Vue.$router.push({ path: "/student" });
                            break;
                    }
                }
                if (res.data.code == 200) {
                    if (res.data.data == "验证码错误") {
                        this._vm.$message("验证码错误或失效");
                    }
                    else if (res.data.data != null && res.data.data != undefined) {
                        //提示
                        this._vm.$message("登录成功");
                    } else {
                        this._vm.$message("XX");
                    }
                } else {
                    this._vm.$message("请求失败");
                }


            })
        },
    },
    mutations: {
        CUT(state, index) {
            switch (index) {
                case 1:
                    state.logingIdentity += state.logingIdentity >= 2 ? -state.logingIdentity : 1
                    break;
                case 2:
                    state.teacherLoginType += state.teacherLoginType >= 1 ? -state.teacherLoginType : 1
                    break;
                case 3:
                    state.adminLoginType += state.adminLoginType >= 1 ? -state.adminLoginType : 1
                    break;

                default:
                    break;
            }
        }
    },
    state: {
        logingIdentity: 0,
        teacherLoginType: 0,
        adminLoginType: 0,
    },
    getters: {

    },
}